/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-27     RT-Thread    first version
 */

#include <rtthread.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include <rtdevice.h>
#include<board.h>
//信号量、处理线程
rt_sem_t sem1;
rt_thread_t th1;
rt_sem_t sem2;
rt_thread_t th2;

//串口设备句柄
rt_device_t serial1;


rt_timer_t tim1;
uint8_t flag = 0;
//1s一次，作为指示灯
void tim1_timeout(void *parameter)
{
    if (flag == 0) {
        flag = flag + 1;
        rt_pin_write(69, PIN_HIGH);
    }
    else {
        rt_pin_write(69, PIN_LOW);
        flag = 0;
    }

    //1s一次释放信号量
//    rt_sem_release(sem1);
//    rt_sem_release(sem2);
}


//发送中断回调函数
rt_err_t tx_tx_done(rt_device_t dev,void *buffer)
{
    //发送完成后释放一个信号量
    rt_sem_release(sem1);
    return RT_EOK;
}

//接收回调函数
rt_err_t rt_rx_ind(rt_device_t dev, rt_size_t size)
{
    //接收完成后也释放一个信号量
    rt_sem_release(sem2);
    return RT_EOK;
}



//发送中断处理线程 sem1
void th1_entry(void *parameter)
{
    static rt_uint8_t number = 0;
    while(1)
    {
        /* 永久方式等待信号量，获取到信号量，则执行 number 自加的操作 */
        rt_sem_take(sem1, RT_WAITING_FOREVER);
        number++;
        rt_kprintf("t1 take a dynamic semaphore. number = %d\n" ,number);

    }
}

//串口接收数据处理线程
//接收数据的缓存
char RxBuff;
void th2_entry(void *parameter)
{
    while(1)
    {
        //不等于1 说明没有接收到数据
        while(rt_device_read(serial1, 0, &RxBuff, 1)!=1)
        {
            //没有接收到数据进来等待接收信号量
            rt_sem_take(sem2, RT_WAITING_FOREVER);
            rt_kprintf("888");
        }
        //接收到数据，不进入while循环  ，接收到数据存在RxBuff中
        //-----------后续处理--------------

        //------------------------------
    }

}


int main(void)
{

    //PIN设备
    rt_pin_mode(69, PIN_MODE_OUTPUT);
    rt_pin_write(69, PIN_LOW);

    //创建timer
    tim1 = rt_timer_create("tim1", tim1_timeout, NULL, 1000, RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER);
    //开启timer
    rt_timer_start(tim1);


    //串口设备
    //查找串口设备
    serial1 = rt_device_find("uart1");

    //配置串口设备
    struct serial_configure serial1_config = RT_SERIAL_CONFIG_DEFAULT;
    rt_device_control(serial1, RT_DEVICE_CTRL_CONFIG, &serial1_config);

    //打开串口设备 中断接收发送
    rt_device_open(serial1, RT_DEVICE_FLAG_INT_TX|RT_DEVICE_FLAG_INT_RX);




    //接收、发送的回调函数  ， 暂时不能用  不知道哪里出了问题
    //----------------------------------------------------
    //设置一个发送回调函数
    rt_err_t ret1;
    ret1 = rt_device_set_tx_complete(serial1,tx_tx_done);
    if (ret1<0) {
        LOG_E("set1 failed...");
    }
    else {
        LOG_D("set1 success...");
    }

    //设置一个接收回调函数
    rt_err_t ret2;
    ret2 = rt_device_set_rx_indicate(serial1, rt_rx_ind);
    if (ret2<0) {
            LOG_E("set2 failed...");
        }
        else {
            LOG_D("set2 success...");
        }
    //----------------------------------------------------


    //信号量  发送
    sem1 = rt_sem_create("sem1", 0, RT_IPC_FLAG_PRIO);

    th1 = rt_thread_create("th1_sem", th1_entry, NULL, 256, 20, 5);
    rt_thread_startup(th1);

    //信号量 接收
    sem2 = rt_sem_create("sem2", 0, RT_IPC_FLAG_PRIO);

    th2 = rt_thread_create("th2_sem", th2_entry, NULL, 256, 20, 5);
    rt_thread_startup(th2);




    //发送一个数据
    char dataTx[] = {"TxTest!\n"};
    rt_device_write(serial1, 0, dataTx, sizeof(dataTx));


    //互斥量

    return RT_EOK;
}












































